/*
 * This computer program is the confidential information and proprietary trade
 * secret of Apptonelabs, Inc. Possessions and use of this program must
 * conform strictly to the license agreement between the user and
 * Apptonelabs, Inc., and receipt or possession does not convey any rights
 * to divulge, reproduce, or allow others to use this program without specific
 * written authorization of Apptonelabs, Inc.
 * 
 * Copyright (c) 2012-2015 Apptonelabs, Inc. All Rights Reserved.
 */
package com.apptonelabs.vnc.vphone;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.io.IOException;

//
// VncCanvas2 is a special version of VncCanvas which may use Java 2 API.
//
class VncCanvas2 extends VncCanvas implements MouseWheelListener {

	private static final long serialVersionUID = 1L;

	public VncCanvas2(VPhone v) throws IOException {
		super(v);
		disableFocusTraversalKeys();
		addMouseWheelListener(this);
	}

	public VncCanvas2(VPhone v, int maxWidth_, int maxHeight_)
			throws IOException {

		super(v, maxWidth_, maxHeight_);
		disableFocusTraversalKeys();
		addMouseWheelListener(this);
	}

	public void mouseWheelMoved(MouseWheelEvent e) {
		if (viewer.rfb != null && rfb.inNormalProtocol) {
			if (inputEnabled) {
				try {
					synchronized (rfb) {
						rfb.writePointerEvent((MouseEvent) e, true, true,
								e.getWheelRotation());
						rfb.writePointerEvent((MouseEvent) e, false, true,
								e.getWheelRotation());
					}
				} catch (IOException ex) {
				}
			}
		}

	}

	public void paintScaledFrameBuffer(Graphics g) {
		Graphics2D g2d = (Graphics2D) g;
		g2d.setRenderingHint(RenderingHints.KEY_RENDERING,
				RenderingHints.VALUE_RENDER_QUALITY);
		g2d.drawImage(memImage, 0, 0, scaledWidth, scaledHeight, null);
	}

	//
	// Try to disable focus traversal keys (JVMs 1.4 and higher).
	//

	private void disableFocusTraversalKeys() {
		try {
			Class[] argClasses = { Boolean.TYPE };
			java.lang.reflect.Method method = getClass().getMethod(
					"setFocusTraversalKeysEnabled", argClasses);
			Object[] argObjects = { new Boolean(false) };
			method.invoke(this, argObjects);
		} catch (Exception e) {
		}
	}

}
